package com.aaa.config;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RoundRobinRule;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.util.stream.Collectors;



public class RibbonConfig {

    public RestTemplate  restTemplate(){
        return  new RestTemplate();
    }


    public IRule  rule(){
        return new RoundRobinRule();
    }

    /***
     * Spring Cloud Gateway 是基于 WebFlux 的，是 ReactiveWeb，所以 HttpMessageConverters 不会自动注入。
     * 于是自己在配置文件中，直接复制源码的 Bean
     * @param converters
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public HttpMessageConverters messageConverters(ObjectProvider<HttpMessageConverter<?>> converters) {
        return new HttpMessageConverters((HttpMessageConverter<?>) converters.orderedStream().collect(Collectors.toList()));
    }
}
